package com.sunwayworld.basemodule.coredata.test.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.sunwayworld.framework.data.CaseInsensitiveLinkedMap;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestValidationResultBean;
import com.sunwayworld.framework.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.coredata.test.dao.LimsTestSupplementDao;
import com.sunwayworld.basemodule.coredata.test.bean.LimsTestSupplementBean;
import com.sunwayworld.basemodule.coredata.test.service.LimsTestSupplementService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.io.Serializable;
import java.util.Map;

@Repository
@GikamBean
public class LimsTestSupplementServiceImpl implements LimsTestSupplementService {

    @Autowired
    private LimsTestSupplementDao limsTestSupplementDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsTestSupplementDao getDao() {
        return limsTestSupplementDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsTestSupplementBean limsTestSupplement = jsonWrapper.parseUnique(LimsTestSupplementBean.class);
        limsTestSupplement.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsTestSupplement);
        return limsTestSupplement.getId();
    }

    @Override
    public RestValidationResultBean validateUniqueByTest(Long testId, RestJsonWrapperBean wrapper) {
        String validateUniqueFieldJson = wrapper.getParamValue("vu");
        if (null == validateUniqueFieldJson) {
            return new RestValidationResultBean(true);
        } else {
            Map<String, String> validateUniqueFieldMap = JSONObject.parseObject(validateUniqueFieldJson, new TypeReference<Map<String, String>>() {
            });
            String information = validateUniqueFieldMap.get("information");
            LimsTestSupplementBean limsTestSupplementBean = selectFirstByFilter(SearchFilter.instance().match("testId", testId).filter(MatchPattern.EQ)
                    .match("information", information).filter(MatchPattern.EQ));
            if (limsTestSupplementBean == null) {
                return new RestValidationResultBean(true);
            }
            return new RestValidationResultBean(false, "core.validator.column.not-unique");
        }
    }
}
